package foundation;

import java.sql.*;

//import oracle.jdbc.driver.*;

public class Oracle {
	
	public Oracle() {

		//conn = CCConnection.conn;
	}
	
	public String getValueByColumnName(Connection conn, String sColumn, String sTable, String sWhere){
		try {
			PreparedStatement ps = conn.prepareStatement(
					"select " + sColumn +
					"from " + sTable +
					"where " + sWhere);
			ResultSet rs = ps.executeQuery();
			
			while (rs.next()){
				System.out.println(rs.getString(sColumn));
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}
	

	
	public ResultSet getSQLResult(Connection conn, String sSQL){
		try {
			PreparedStatement ps = conn.prepareStatement(sSQL);
			ResultSet rs = ps.executeQuery();
			while (rs.next()){
				System.out.println(rs.getString("FUNC_NAME"));
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}
	
	
    public long getCount(Connection conn, String sTable, String sWhere){
        String sSQL = "";
        long lNum = 0;
        // build SQL string
        sSQL = "select count(*)" + 
        " from " + sTable +
        " Where " + sWhere;
        
       
        try {
            PreparedStatement ps = conn.prepareStatement(sSQL);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                lNum = Long.parseLong(rs.getString(1));
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return lNum;
    }	
    
    public double getSum(Connection conn, String sColumn, String sTable, String sWhere){
        String sSQL = "";
        double dNum = 0;
        // build SQL string
        sSQL = "select sum(" + sColumn +")" + 
        " from " + sTable +
        " Where " + sWhere;

        try {
            Statement ps = conn.createStatement();
            ResultSet rs = ps.executeQuery(sSQL);
            while (rs.next()){
                dNum = Double.parseDouble(rs.getString(1));
            }
            rs.close();
            ps.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return dNum;
    }   
}
